package cn.hfuu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * @author oldliu
 * @since 1.0
 */
public class WriteHDFS {
    public static void main(String[] args) throws IOException {
        String url = "hdfs://hadoopmaster:9000";
        Configuration conf = new Configuration();
//指定使用HDFS访问
        conf.set("fs.defaultFS", url);
//进行客户端身份的设置(root为虚拟机的用户名，hadoop集群节点的其中一个都可以)
        System.setProperty("HADOOP_USER_NAME", "root");
// 通过FiLeSystem的静态get()方法获取HDFS文件系统客户端对象
        FileSystem fs = FileSystem.get(conf);
        Path path = new Path("/heda2022/dashuju22ji-1.txt");

        // 创建输出流
        OutputStream os = fs.create(path, true);
        PrintWriter bos = new PrintWriter(os);
        bos.write("张梓旭\n" +
                "孙伟翔\n" +
                "冯翔\n" +
                "丁梦幻\n" +
                "金夕楠\n" +
                "朱望新\n" +
                "朱蓉\n" +
                "赵政\n" +
                "薛五恒\n" +
                "徐森昊\n" +
                "裴梦缘\n" +
                "石少泽\n" +
                "张睿\n" +
                "陆章宇\n" +
                "曾泽威\n" +
                "晚滔\n" +
                "顾明健\n" +
                "杜运亳\n" +
                "蔡杭潼\n" +
                "唐凤颖\n" +
                "杨光耀\n" +
                "孙宇\n" +
                "范鑫锐\n" +
                "钱慧琴\n" +
                "高博\n" +
                "冯英杰\n" +
                "张凯日\n" +
                "黄东明\n" +
                "田启勇\n" +
                "李梦萍\n" +
                "侯力源\n" +
                "胡本爽\n" +
                "廖佳怡\n");

        bos.close();
        fs.close();
        System.out.println("写入成功");
    }


}
